package com.example.demo.web;

import com.example.demo.common.Constant;
import com.example.demo.domain.User;
import com.example.demo.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by jinmingjiang on 2017/9/14.
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    JwtUtil jwtUtil;

    @PostMapping("/login")
    public String login(@RequestBody User loginUser, HttpServletResponse response) throws Exception {
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();

        User user = new User();
        user.setId(1L);
        user.setMobile("133333333");

        String subject = JwtUtil.generalSubject(user);
        String jwt = "";
        if ("jmj".equals(username) && "123".equals(password)) {
            jwt = jwtUtil.createJWT(Constant.JWT_ID, subject, Constant.JWT_TTL);
        }
        Cookie cookie = new Cookie("token",jwt);
        cookie.setPath("/");
        response.addCookie(cookie);
        // mongod --config D:\tools\MongoDB\Server\3.4\mongo.config
        return jwt;
    }
}
